package Presentacion;

import Clases.AdelantoAguinaldo;
import Clases.Empleado;
import Clases.Liquidacion;
import Clases.Pago;
import Clases.Prestamo;
import Clases.crearPDF;
import Control.ControlAdelantoAguinaldo;
import Control.ControlEmpleado;
import Control.ControlPago;
import Control.ControlPrestamo;
import Control.ControlVacaciones;
import de.javasoft.plaf.synthetica.SyntheticaLookAndFeel;
import java.io.File;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class LiquidacionCesantia extends javax.swing.JFrame {

    ControlEmpleado controlE = ControlEmpleado.obtenerInstancia();
    ControlPago controlP = ControlPago.obtenerInstancia();
    ControlAdelantoAguinaldo controlAdelanto = ControlAdelantoAguinaldo.obtenerInstancia();
    ControlPrestamo controlPrestamo = ControlPrestamo.obtenerInstancia();
    ControlVacaciones controlV = ControlVacaciones.obtenerInstancia();
    ArrayList<Pago> pagos = null;
    private ImageIcon iconoError;

    private LiquidacionCesantia() {
        initComponents();

    }
    private static LiquidacionCesantia instancia = null;

    public static LiquidacionCesantia obtenerInstancia() {
        if (instancia == null) {
            instancia = new LiquidacionCesantia();
        }
        return instancia;
    }

    public void init() {
        setTitle("Liquidación y Cesantía");
        setLocationRelativeTo(null);
        UIManager.removeAuxiliaryLookAndFeel(UIManager.getLookAndFeel());
        SwingUtilities.updateComponentTreeUI(this);
        try {
            UIManager.removeAuxiliaryLookAndFeel(UIManager.getLookAndFeel());
            SyntheticaLookAndFeel.setWindowsDecorated(false);
            UIManager.setLookAndFeel("de.javasoft.plaf.synthetica.SyntheticaAluOxideLookAndFeel");
            SwingUtilities.updateComponentTreeUI(this);

        } catch (Exception ex) {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        this.limpiar();
        inicioTooltops();
        this.setVisible(true);
    }

    private void ocultarLabels() {
        jLabelOpcion.setVisible(false);
        jLabelEmpleado.setVisible(false);
        jLabelFechaSalida.setVisible(false);
    }

    private void inicioTooltops() {
        iconoError = new ImageIcon(getClass().getResource("/Imagenes/warning.png"));

        jLabelOpcion.setIcon(iconoError);
        jLabelOpcion.setToolTipText("Debe seleccionar una opción.");

        jLabelEmpleado.setIcon(iconoError);
        jLabelEmpleado.setToolTipText("Debe buscar un empleado.");

        jLabelFechaSalida.setIcon(iconoError);
        jLabelFechaSalida.setToolTipText("Debe definir una fecha de salida");

        ocultarLabels();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane5 = new javax.swing.JScrollPane();
        jTable5 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel19 = new javax.swing.JLabel();
        jDateChooser1 = new com.toedter.calendar.JDateChooser();
        jLabel2 = new javax.swing.JLabel();
        jButton8 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jComboBox2 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jLabelFechaSalida = new javax.swing.JLabel();
        jLabelEmpleado = new javax.swing.JLabel();
        jLabelOpcion = new javax.swing.JLabel();
        labelCedula = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        labelNombre = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        labelCodigo = new javax.swing.JLabel();

        jTable5.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Cédula", "Nombre", "Descripcion", "Valor", "Eliminar a"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane5.setViewportView(jTable5);

        setMinimumSize(new java.awt.Dimension(570, 340));
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel5.setText("Liquidación & Cesantía");

        jLabel18.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel18.setText("Opción  a realizar: ");

        jButton4.setText("Cancelar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton3.setText("Aplicar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel19.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel19.setText("Fecha de salida:");

        jDateChooser1.setDateFormatString("dd-MM-yyyy");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setText("Cédula empleado:");

        jButton8.setText("Buscar Empleado");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("Fecha de ingreso:");

        jTextField2.setEditable(false);

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Elija una opción:", "Liquidación sin Cesantía.", "Liquidación con Cesantía." }));
        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox2ActionPerformed(evt);
            }
        });

        jButton1.setText("Ver liquidaciones y Cesantias generadas");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabelFechaSalida.setText("x");
        jLabelFechaSalida.setMaximumSize(new java.awt.Dimension(24, 24));
        jLabelFechaSalida.setMinimumSize(new java.awt.Dimension(22, 22));
        jLabelFechaSalida.setPreferredSize(new java.awt.Dimension(22, 22));

        jLabelEmpleado.setText("x");
        jLabelEmpleado.setMaximumSize(new java.awt.Dimension(24, 24));
        jLabelEmpleado.setMinimumSize(new java.awt.Dimension(22, 22));
        jLabelEmpleado.setPreferredSize(new java.awt.Dimension(22, 22));

        jLabelOpcion.setText("x");
        jLabelOpcion.setMaximumSize(new java.awt.Dimension(24, 24));
        jLabelOpcion.setMinimumSize(new java.awt.Dimension(22, 22));
        jLabelOpcion.setPreferredSize(new java.awt.Dimension(22, 22));

        labelCedula.setText("__________________");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel4.setText("Nombre empleado:");

        labelNombre.setText("__________________");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("Código empleado");

        labelCodigo.setText("__________________");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel18)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabelOpcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(118, 118, 118)
                                .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabelFechaSalida, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(137, 137, 137)
                        .addComponent(jLabel5)))
                .addGap(0, 165, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jButton1)
                                .addGap(101, 101, 101)
                                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(labelCedula, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jButton8)
                                .addGap(10, 10, 10)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton4)
                            .addComponent(jLabelEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel19)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(labelCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(labelNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(37, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel18)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelOpcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(labelCedula)
                    .addComponent(jButton8)
                    .addComponent(jLabelEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(labelCodigo))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(labelNombre))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel19, javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jDateChooser1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabelFechaSalida, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jButton4)
                    .addComponent(jButton1))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        limpiar();
        dispose();
    }//GEN-LAST:event_jButton4ActionPerformed

    private boolean validacion() {
        ocultarLabels();
        boolean itsOk = true;

        int tipo = jComboBox2.getSelectedIndex();
        if (tipo == 0) {
            itsOk = false;
            jLabelOpcion.setVisible(true);
        }
        String cedula = labelCedula.getText().trim();
        if (Character.isDigit(cedula.charAt(0)) == false) {
            itsOk = false;
            jLabelEmpleado.setVisible(true);
        }

        Date fechaSalida = jDateChooser1.getDate();
        if (fechaSalida == null) {
            itsOk = false;
            jLabelFechaSalida.setVisible(true);
        }

        return itsOk;
    }

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        Date fechaSalida = jDateChooser1.getDate();
        int tipo = jComboBox2.getSelectedIndex();
        if (validacion()) {
            //if (fechaSalida != null && tipo != 0) {
            int confirmado = JOptionPane.showConfirmDialog(null, "¿Está seguro de que desea realizar liquidación a el empleado seleccionado?");
            if (JOptionPane.OK_OPTION == confirmado) {
                double total = 0;
                String detalle = "";
                Long cedulaEmpledo = Long.valueOf(labelCedula.getText());
                Empleado empleado = controlE.getEmpleadoCedula(cedulaEmpledo);

                empleado.setFechaFin(fechaSalida); // le pongo la fecha fin !!
                controlE.modificarEmpleado(empleado);
                switch (tipo) {
                    case 1:
                        total = calcularLiquidacion(empleado);
                        detalle = "liquidacion";
                        break;
                    case 2:
                        total = calcularCesantia(empleado);
                        detalle = "cesantia";
                        break;
                }
                Liquidacion liquidacion = new Liquidacion(empleado, fechaSalida, total, detalle);// guarda tamb cesantia aqui
                controlP.guardarLiquidacionCesantia(liquidacion);
                //this.generarReporte(empleado, detalle, total);
                this.limpiar();
                dispose();
            }

        }
    }//GEN-LAST:event_jButton3ActionPerformed
    public void generarReporte(Empleado empleado, String detalle, double total) {
        crearPDF pdf = new crearPDF();
        ArrayList<String> contenidoPDF = new ArrayList<String>();

        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new java.io.File("."));
        chooser.setDialogTitle("Elegir Destino");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);

        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {// faltaria elegir formato , aunq creo q al ser una liquidacion mejor solo pdf
            File directorio = chooser.getSelectedFile();
            String ruta = directorio.getPath();
            //generar reporte respectivo
            contenidoPDF.add(" ");
            contenidoPDF.add(" ");
            contenidoPDF.add("Fecha de Ingreso: " + empleado.getFechaIngreso());
            contenidoPDF.add("Fecha de Salida: " + empleado.getFechaFin());
            contenidoPDF.add("Nombre del empleado: " + empleado.getNombre() + " " + empleado.getApellido1() + " " + empleado.getApellido2());
            contenidoPDF.add("Cédula: " + empleado.getCedula());
            contenidoPDF.add(" ");
            contenidoPDF.add(" ");
            contenidoPDF.add("Detalles de la " + detalle + ":");
            for (int j = 0; j < pagos.size(); j++) {
                contenidoPDF.add("Mes:" + j + 1 + ": " + pagos.get(j).getSalarioBase());
            }
            contenidoPDF.add("Total: " + total);
           // pdf.crearPDF(String.valueOf(empleado.getCedula()), "Liquidación", contenidoPDF, ruta);
           // JOptionPane.showMessageDialog(null, "PDF generado con toda la información de la " + detalle + " del empleado: "
           //         + empleado.getCedula() + ", " + empleado.getNombre() + " " + empleado.getApellido1(), "Mensaje", JOptionPane.INFORMATION_MESSAGE);
       JOptionPane.showMessageDialog(null, "Liquidación generada con exito!", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        } else {
            System.out.println("No se seleccionó ninguna ruta para guardar el archivo ");
        }
    }

    public void limpiar() {
        labelCedula.setText("_______________________");
        labelNombre.setText("_______________________");
        labelCodigo.setText("_______________________");
        jTextField2.setText("");
        jDateChooser1.setDate(null);
        jComboBox2.setSelectedIndex(0);
        ocultarLabels();
    }

    public double calcularLiquidacion(Empleado empleado) {
        Double totalLiquidacion = 0.0;
        pagos = controlP.getPagosPorNumEmpleado(empleado.getNumEmpleado());
        double suma = 0;
        Double prom = 0.0;
        int sumaAdelantos = 0;
        int sumaPrestamos = 0;
        double totalVacaciones = 0;
        int cantMeses = pagos.size();
        if (cantMeses % 2 != 0) {  // quiere decir cant d mes inexacto x lo cual cuanto hasta mes anterios compeletamente laborado     
            cantMeses = cantMeses - 1;
            pagos.remove(pagos.size() - 1); // remuevo la ultima quincena xq no la tomare en cuenta
        }
        if (cantMeses <= 12) {
            for (int i = 0; i < pagos.size(); i++) {
                suma += pagos.get(i).getSalarioBase2();
            }

            prom = suma / cantMeses;
            if (Double.isNaN(prom)) {
                prom = 0.0;
            }
            ArrayList<AdelantoAguinaldo> adelantos = controlAdelanto.getAdelantoEmpleado(empleado.getNumEmpleado());
            for (int i = 0; i < adelantos.size(); i++) {
                sumaAdelantos += adelantos.get(i).getAdelanto();
            }
            Empleados ref = new Empleados();
            int cantidadVacaciones = ref.calcularVacaciones(empleado.getFechaIngreso(), empleado.getNumEmpleado());
            totalVacaciones = (prom / 30) * cantidadVacaciones; // calculo de monto x vacaciones no tomadas//NaN
            if (Double.isNaN(totalVacaciones)) {
                totalVacaciones = 0.0;
            }
            ArrayList<BigInteger> asignacionPrestamo = controlPrestamo.getAsignacionPrestamo(empleado.getNumEmpleado());
            for (int j = 0; j < asignacionPrestamo.size(); j++) {
                Prestamo encontrado = controlPrestamo.getPrestamoPorCodigo(asignacionPrestamo.get(j).intValue());
                sumaPrestamos += encontrado.getDeuda();
            }
            totalLiquidacion = prom + sumaAdelantos + totalVacaciones + sumaPrestamos;//NaN
            if (Double.isNaN(totalLiquidacion)) {
                totalVacaciones = 0.0;
            }
        }
        return totalLiquidacion;
    }

    public double calcularCesantia(Empleado empleado) {
        double totalLiquidacion = this.calcularLiquidacion(empleado);
        Date fechaIngreso = empleado.getFechaIngreso();
        Date fechaFin = empleado.getFechaFin();
        double cesantia = 0;
        int cantAnosTrabajados = fechaIngreso.getYear() - fechaFin.getYear();
        if (cantAnosTrabajados >= 8) {
            cesantia = totalLiquidacion * 8;
        } else {
            cesantia = totalLiquidacion * cantAnosTrabajados;
        }
        return cesantia;
    }

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        Empleados empleado = new Empleados();
        empleado.init("liquidacion");
    }//GEN-LAST:event_jButton8ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        ConsultarLiquidaciones consulta = ConsultarLiquidaciones.obtenerInstancia();
        consulta.init();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox2ActionPerformed
    public void setLabelsEmpleado(String cedulaEmpleado, String nombreEmpleado, Long codigo) {
        labelCedula.setText(cedulaEmpleado);
        labelNombre.setText(nombreEmpleado);
        labelCodigo.setText(String.valueOf(codigo));

    }

    public void setFechaIngreso(String fechaIngreso) {
        jTextField2.setText(fechaIngreso);
    }

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LiquidacionCesantia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LiquidacionCesantia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LiquidacionCesantia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LiquidacionCesantia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LiquidacionCesantia().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton8;
    private javax.swing.JComboBox jComboBox2;
    private com.toedter.calendar.JDateChooser jDateChooser1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabelEmpleado;
    private javax.swing.JLabel jLabelFechaSalida;
    private javax.swing.JLabel jLabelOpcion;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTable jTable5;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JLabel labelCedula;
    private javax.swing.JLabel labelCodigo;
    private javax.swing.JLabel labelNombre;
    // End of variables declaration//GEN-END:variables
}
